package com.shouhuobao.bhi;

import java.io.Serializable;
import java.text.MessageFormat;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.MainActivity;
import com.collectplus.express.R;
import com.collectplus.express.app.Intents;
import com.collectplus.express.logic.AppCache;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppLogic;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.OrderBean;

/**
 * 接单界面的父类
 * 
 * @author coffee <br>
 *         2016-1-6下午5:08:55
 */
public abstract class OrderBaseActivity extends BaseActivity2 implements View.OnClickListener {

	// 取消订单基础类
	protected Serializable orderId; // 订单ID
	private TextView mCourierName;// 快递员姓名
	protected ImageView mHeadView;// 快递员头像

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onCreate(Bundle savedInstanceState) {
		// 从上一个界面传过来的参数
		orderId = getIntent().getSerializableExtra("orderId");
		super.onCreate(savedInstanceState);
	}

	protected void findViewById() {
		super.findViewById();
		// 打电话按钮
		View phoneLayout = findViewById(R.id.order_courier_call);
		if (phoneLayout != null) {
			phoneLayout.setOnClickListener(this);
		}
		//
		mCourierName = (TextView) findViewById(R.id.order_tip_1);
		mHeadView = (ImageView) findViewById(R.id.order_courier_head);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (orderId != null) {
			AppHttp.getInstance().orderDetail(orderId);
		} else {
			OrderBean order = AppCache.getOrder();
			if (order != null) {
				updateViewData(order);
				// 刷新订单
				AppHttp.getInstance().orderDetail(order.getId() + "");
			} else {// order为空的时候重新加载
				showLoadingDialog(null);
				int orderId = AppCache.getOrderId();
				AppHttp.getInstance().orderDetail(orderId);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// // 我的包裹
		// case R.id.order_receiver_layout:
		// Intent intent = new Intent();
		// // intent.setClass(getContext(), ReceiverListActivity.class);
		// intent.setClass(getContext(), OrderReceiverActivity.class);
		// startActivity(intent);
		// break;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		// 取消订单
		case MsgID.order_cancel:
			cancelLoadingDialog();
			AppResult<?> result = JSON.parser(msg.obj);
			switch (result.getStatus()) {
			case 1:
				AppCache.removeOrderAlls();
				// Cache.setLocationItems(null);
				getContext().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Intent intent = new Intent();
						intent.setClass(getContext(), MainActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
						finish();// 关闭当前Activity
					}
				});
				break;
			default:
				showToast(result.getMessage());
				break;
			}
			return true;
			// 刷新订单
		case MsgID.order_detail:
			cancelLoadingDialog();
			final AppResult<OrderBean> appResult = JSON.parser(msg.obj, OrderBean.class);
			if (appResult.getStatus() == 1) {
				OrderBean order = appResult.getResult();
				if (order.getCourier() != null && order.getCourier().getImagePath() != null) {
					String imageUrl = order.getCourier().getImagePath();
					AppLogic.getInstance().loadImage(imageUrl, mHeadView);
				}
				AppCache.setOrder(appResult.getResult());
				getContext().runOnUiThread(new Runnable() {
					@Override
					public void run() {
						OrderBean order = appResult.getResult();
						updateViewData(order);
						// 如果是立即下单 或者 预约服务。并且已经在上门时间段了
						if (order.isImmediate() || System.currentTimeMillis() > order.getServiceTimeGe()) {
							if (order.getStatus() < 3) {// 1449217332697 > 1449216000000
								order.setStatus(3);
							}
						}
						// MainFragmentActivity.getInstance().handleHomePage(order.getId(), order.getStatus());
					}
				});
			} else {
				showToast(appResult.getMessage());
			}
			break;
		default:
			break;
		}
		return false;
	}

	/**
	 * 获取订单数据后刷新view
	 * 
	 * @param order
	 */
	protected void updateViewData(OrderBean order) {
		if (mCourierName != null && order != null && order.getCourier() != null) {
			String formatStr = "炫酷的快递员{0}正在途中";
			String text = MessageFormat.format(formatStr, order.getCourier().getName());
			mCourierName.setText(text);
			mCourierName.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void finish() {
		super.finish();
		AppCache.setParcelNew(null);
		Intents.startMainActivityClearTop(getContext());
		overridePendingTransition(0, R.anim.out_top2bottom);
	}
}
